package com.alibaba.algo.controller.user;

import com.alibaba.algo.cooperation.usercomponent.vo.req.user.SelectRoleListRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.SelectRoleListResponseVO;
import com.alibaba.algo.core.base.PagerEntity;
import com.alibaba.algo.domain.user.SysAppDomain;
import com.alibaba.algo.domain.user.SysRoleDomain;
import com.alibaba.algo.entity.user.SysApp;
import com.alibaba.algo.exception.SysPolicyErrorCode;
import com.aliyun.gsuite.api.IClientForRole;
import com.aliyun.gsuite.model.CapRoleInfo;
import com.aliyun.gsuite.response.BaseResult;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 类SysRoleController.java的实现描述：角色 Controller
 *
 * @author nieganggang 2021-03-12 16:08:17
 */
@RestController
public class SysRoleAdapterController implements IClientForRole {

    @Autowired
    SysRoleDomain sysRoleDomain;
    @Autowired
    SysAppDomain sysAppDomain;

    @Override
    public BaseResult<Page<CapRoleInfo>> listRoles(String appId, Integer pageNo, Integer pageSize) {
        SelectRoleListRequestVO requestVO = new SelectRoleListRequestVO();
        SysApp sysApp = sysAppDomain.getById(Long.valueOf(appId));
        if (sysApp == null) {
            return BaseResult.result(SysPolicyErrorCode.appNotExist.name(), SysPolicyErrorCode.appNotExist.getMessage());
        }
        requestVO.setAppCode(sysApp.getAppCode());
        requestVO.setPageIndex(pageNo);
        requestVO.setPageSize(pageSize);
        PagerEntity<SelectRoleListResponseVO> entity = sysRoleDomain.selectPage(requestVO);
        Page<CapRoleInfo> pages = new Page<>();
        if (entity != null && entity.getDataList() != null) {
            for (SelectRoleListResponseVO response : entity.getDataList()) {
                CapRoleInfo capRoleInfo = new CapRoleInfo();
                capRoleInfo.setRoleId(String.valueOf(response.getId()));
                capRoleInfo.setRoleName(response.getRoleName());
                capRoleInfo.setRoleCode(response.getRoleCode());
                capRoleInfo.setRoleDesc(response.getRoleDesc());
                pages.add(capRoleInfo);
            }
        }
        return BaseResult.success(pages.size(), pages);
    }
}
